package com.eurotalkweather;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;





public class SAXHandlerCities extends DefaultHandler {
    
    private String[][] citiesList;
    private String[] citiesNameList;
    private int citiesNumber;
    
    
    
    
    
    SAXHandlerCities() {
	citiesNumber = 0;
	citiesList = new String[5000][4]; //Currently the number of sites is 4912.
	citiesNameList = new String[5000];
    }
    
    
    
    
    
    @Override
    public void error(SAXParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
    
    
    
    
    
    @Override
    public void warning(SAXParseException e) {
        System.out.println("Warning: " + e.getMessage());
    }
    
    
    
    
    
    @Override
    public void startElement(String uri, String qName, String elementName, Attributes attributesList)
	    throws SAXException {
        if (elementName.equals("Location")) {
            citiesList[citiesNumber][0] = attributesList.getValue(0); //id
            citiesList[citiesNumber][1] = attributesList.getValue(1); //longitude
            citiesList[citiesNumber][2] = attributesList.getValue(2); //latitude
            citiesList[citiesNumber][3] = attributesList.getValue(3); //name
            citiesNameList[citiesNumber] = attributesList.getValue(3); //name
            citiesNumber++;
        }
    }
    
    
    
    
    
    @Override
    public void endDocument() throws SAXException {
	String[] citiesNameListProvi = new String[getCitiesNumber()];
	for (int n=0;citiesNameList[n]!=null;n++) citiesNameListProvi[n]=citiesNameList[n];
	citiesNameList = null;
	citiesNameList = citiesNameListProvi;
    }
    
    
    
    
    
    public String[][] getCitiesList() {
	return citiesList;
    }
    
    
    
    
    
    public String[] getCitiesNameList() {
	return citiesNameList;
    }
    
    
    
    
    public int getCitiesNumber() {
	return citiesNumber;
    }
}